package zhongbo.java.air.model;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import zhongbo.java.air.config.JacksonConfig;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

/**
 * 飞行轨迹记录表(AirFlightPath)实体类
 *
 * @author makejava
 * @since 2025-04-02 09:30:54
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("air_flight_path")
@ApiModel("航迹信息")
public class AirFlightPath implements Serializable {
    /**
     * UUID 主键
     */
    @ApiModelProperty(value = "UUID 主键", example = "1")
    @TableId(type = IdType.ASSIGN_UUID)
    @NotNull(message = "UUID 主键不能为空")
    @Size(min = 1, max = 36, message = "UUID主键长度应在1到36个字符之间")
    private String id;

    /**
     * 关联飞机ICAO码
     */
    @ApiModelProperty(value = "关联飞机ICAO码", example = "122")
    @NotNull(message = "关联飞机ICAO码不能为空")
    @Size(min = 1, max = 6, message = "关联飞机ICAO码长度应在1到6个字符之间")
    @Pattern(regexp = "^[A-Z0-9]{1,6}$", message = "关联飞机ICAO码必须是1到6位的字母或数字")
    @JsonProperty("icao_code")
    private String icaoCode;

    /**
     * 时间戳（精确到毫秒）
     */
    @ApiModelProperty(value = "时间戳（精确到毫秒）", example = "2025-03-31 15:19:25.123")
    @NotNull(message = "时间戳（精确到毫秒）不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS", timezone = "GMT+8")
    @JsonDeserialize(using = JacksonConfig.CustomDateDeserializer.class)
    @JsonProperty("timestamp")
    private Date timestamp;

    /**
     * 纬度（WGS84坐标系）
     */
    @ApiModelProperty(value = "纬度（WGS84坐标系）", example = "12")
    @JsonProperty("latitude")
    private Double latitude;

    /**
     * 经度（WGS84坐标系）最多9位，有小数点后6位
     */
    @ApiModelProperty(value = "经度（WGS84坐标系）", example = "13")
    @JsonProperty("longitude")
    private Double longitude;

    /**
     * 海拔高度（米）
     */
    @ApiModelProperty(value = "海拔高度（米）", example = "12")
    @JsonProperty("altitude")
    private Integer altitude;

    /**
     * 地速（公里/小时）
     */
    @ApiModelProperty(value = "地速（公里/小时）", example = "12")
    @JsonProperty("speed")
    private Integer speed;

    /**
     * 航向角（0-359度）
     */
    @ApiModelProperty(value = "航向角（0-359度）", example = "12")
    @JsonProperty("heading")
    private Integer heading;

    /**
     * 数据源类型（枚举值：ADS_B, RADAR, MLAT）
     */
    @ApiModelProperty(value = "数据源类型（枚举值：ADS_B, RADAR, MLAT）", example = "ADS_B")
    @NotNull(message = "数据源类型（枚举值：ADS_B, RADAR, MLAT）不能为空")
    @Size(min = 1, max = 20, message = "数据源类型（枚举值：ADS_B, RADAR, MLAT）长度应在1到20个字符之间")
    @Pattern(regexp = "^(ADS_B|RADAR|MLAT)$", message = "数据源类型（枚举值：ADS_B, RADAR 或 MLAT）必须是ADS_B, RADAR 或 MLAT")
    @JsonProperty("source_type")
    private String sourceType;

    // 添加默认值处理
    public void ensureDefaults() {
        if (this.id == null) {
            this.id = UUID.randomUUID().toString();
        }
        if (this.timestamp == null) {
            this.timestamp = new Date();
        }
        if (this.sourceType == null) {
            this.sourceType = "UNKNOWN"; // 设置默认值为 "UNKNOWN"
        }
        // 添加对 latitude, longitude, altitude, speed, heading 的默认值处理
        if (this.latitude == null) {
            this.latitude = 0.0;
        }
        if (this.longitude == null) {
            this.longitude = 0.0;
        }
        if (this.altitude == null) {
            this.altitude = 0;
        }
        if (this.speed == null) {
            this.speed = 0;
        }
        if (this.heading == null) {
            this.heading = 0;
        }
        // 添加对 icao_code 的默认值处理
        if (this.icaoCode == null || this.icaoCode.isEmpty()) {
            this.icaoCode = "UNKWN"; // 原默认值"UNKNOWN"长度7，改为6字符的"UNKWN"
        } else if (this.icaoCode.length() > 6) {
            this.icaoCode = this.icaoCode.substring(0, 6); // 截断超过6个字符的部分
        }
    }
}